package com.example.yangguoying.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.yangguoying.R;
import com.example.yangguoying.entity.StudentInfo;

import java.util.List;

public class StudentInfoAdapter extends BaseAdapter {
    List<StudentInfo> list;
    Context context;

    public StudentInfoAdapter(Context context,List<StudentInfo> list) {
        this.list = list;
        this.context = context;
    }

    public void setList(List<StudentInfo> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView  = LayoutInflater.from(context).inflate(R.layout.student_info_item,null);
            viewHolder.student_name = convertView.findViewById(R.id.student_name);
            viewHolder.student_num = convertView.findViewById(R.id.student_num);
            viewHolder.student_phone = convertView.findViewById(R.id.student_phone);
            viewHolder.call_phone = convertView.findViewById(R.id.call_phone);
            viewHolder.upload_button = convertView.findViewById(R.id.upload_button);
            viewHolder.edit_button = convertView.findViewById(R.id.edit_button);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.student_name.setText("姓名：" + list.get(position).getStudentName());
        viewHolder.student_num.setText("学号：" + list.get(position).getStudentNum());
        viewHolder.student_phone.setText(list.get(position).getStudentPhone());
        if (list.get(position).getUpload() == 1){
            viewHolder.upload_button.setBackgroundResource(R.drawable.upload_icon);
        }else {
            viewHolder.upload_button.setBackgroundResource(R.drawable.no_upload_icon);
        }

        viewHolder.edit_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onModifyListener.onModifyClick(position);
            }
        });

        viewHolder.call_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onCallPhoneListener.onCallPhoneClick(position);
            }
        });

        viewHolder.upload_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onUploadListener.onUploadClick(position);
            }
        });


        return convertView;
    }

    class ViewHolder{
        TextView student_name;
        TextView student_num;
        TextView student_phone;
        ImageButton call_phone;
        Button upload_button;
        Button edit_button;

    }

    public interface OnModifyListener {
        void onModifyClick(int i);
    }
    private OnModifyListener onModifyListener;
    public void setOnItemClickModifyListener(OnModifyListener onModifyListener) {
        this.onModifyListener = onModifyListener;
    }

    public interface OnCallPhoneListener {
        void onCallPhoneClick(int i);
    }
    private OnCallPhoneListener onCallPhoneListener;
    public void setOnItemClickCallPhoneListener(OnCallPhoneListener onCallPhoneListener) {
        this.onCallPhoneListener = onCallPhoneListener;
    }

    public interface OnUploadListener {
        void onUploadClick(int i);
    }
    private OnUploadListener onUploadListener;
    public void setOnItemClickUploadListener(OnUploadListener onUploadListener) {
        this.onUploadListener = onUploadListener;
    }


}
